
import { login, getUserInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'

const state = {
  token: getToken() || '', // 判断本地是否有cookie的token
  UserInfo: null // 用户信息
}

const mutations = {
  setToken(state, data) {
    // 把token存在本地的cookie
    setToken(data)
    state.token = data
  },
  setUserInfo(state, data) {
    state.UserInfo = data
  },
  // 退出登录
  async logout(state) {
    // 清除数据
    state.token = ''
    state.UserInfo = null
    removeToken()
  }
}

const actions = {
  async login(store, data) {
    const res = await login(data) // res=res.data.data
    store.commit('setToken', res.data.jwt)
  },
  async getUserInfo(store) {
    const res = await getUserInfo()
    // 获取用户基本信息
    store.commit('setUserInfo', res.data)
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

